package com.jdd.common.util;

import lombok.extern.slf4j.Slf4j;

/**
 * @author ：jdd孙庆伟
 * @date ：Created in 2021/10/20 14:09
 * @description：字符串使用逗号分隔
 * @modified By：`
 * @version: 1.0
 */
@Slf4j
public class StringSeparatedUtil {

    public static String getSepStr(String str) {
        StringBuffer sb = new StringBuffer();
        char[] ar = str.toCharArray();
        for (int i = 0; i < ar.length; i++) {
            String s = String.valueOf(ar[i]);
            if ("拾".equals(s)) {
                s = "十";
            }
            sb.append(s).append(",");
        }
        String keywordStr = sb.deleteCharAt(sb.length() - 1).toString();
        return keywordStr;
    }

    public static String getSpeDate(String time) {
        String str = DateUtils.turnDayHourMinuteString(time);
        Boolean TBoolean = false;
        Boolean xsBoolean = false;
        Boolean fzBoolean = false;
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            String charStr = String.valueOf(chars[i]);
            if ("天".equals(charStr)) {
                TBoolean = true;
            }
            if ("小".equals(charStr)) {
                xsBoolean = true;
            }
            if ("分".equals(charStr)) {
                fzBoolean = true;
            }
        }
        StringBuffer sb = new StringBuffer();
        //如果存在“天”的的字符串
        if (TBoolean) {
            //截取“天”之前的字符串
            String str1 = str.substring(0, str.indexOf("天"));
            //处理天之前的字符串后拼接逗号跟天
            sb.append(getSepStrQbf(str1)).append(",").append("天").append(",");
        }
        //如果存在"小"的字符串
        if (xsBoolean) {
            //截取“小”之前的字符串
            String str1 = str.substring(0, str.indexOf("小"));
            //截取“天”之后的字符串 截取到“小”之前的数据“天”之后的数据 得到小时数
            String substr = str1.substring(str1.lastIndexOf("天") + 1);
            sb.append(getSepStrQbf(substr)).append(",").append("小时").append(",");
        }
        //如果存在“分”的字符串
        if (fzBoolean) {
            //截取“分”之前的字符串
            String str1 = str.substring(0, str.indexOf("分"));
            String substr = null;

            //判断是否是“5天13分”这种数据 如果不存在“小”则是
            if (!xsBoolean) {
                //截取“分”之前的数据，“天”之后的字符串 得到分钟数
                substr = str1.substring(str1.lastIndexOf("天") + 1);
            } else {
                //截取“时”之后的字符串 截取到“分”之前的数据“时”之后的数据 得到分钟数
                substr = str1.substring(str1.lastIndexOf("时") + 1);
            }
            sb.append(getSepStrQbf(substr)).append(",").append("分").append(",");
        }
        //截取最后一位的逗号
        String keywordStr = sb.deleteCharAt(sb.length() - 1).toString();
        return keywordStr;
    }

    /**
     * 处理数字 拼接 千 百 十
     *
     * @param str 需要处理的数字
     * @return java.lang.String
     * @author jdd孙庆伟
     * @date 2021/10/28 09:13:51
     * @version 1.0
     */
    public static String getSepStrQbf(String str) {
        log.info(str);
        StringBuffer sb = new StringBuffer();
        //字符串拆分char数组
        char[] tChars = str.toCharArray();
        for (int i = 0; i < tChars.length; i++) {
            //当前下标的字符
            String aChar = String.valueOf(tChars[i]);
            //获取数字长度 用于判断拼接
            Integer length = tChars.length;
            //如果大于等于4代表千位数则拼接千百十个
            if (length >= 4) {
                if (i == 0) {
                    String qian = aChar + ",千,";
                    sb.append(qian);
                }
                if (i == 1) {
                    String bai = aChar + ",百,";
                    sb.append(bai);
                }
                if (i == 2) {
                    String shi = aChar + ",十,";
                    sb.append(shi);
                }
                if (i == 3) {
                    sb.append(aChar);
                }
                continue;
            }
            //如果大于等于3代表百位数则拼接百十个
            if (length >= 3) {
                if (i == 0) {
                    String bai = aChar + ",百,";
                    sb.append(bai);
                }
                if (i == 1) {
                    String shi = aChar + ",十,";
                    sb.append(shi);
                }
                if (i == 2) {
                    sb.append(aChar);
                }
                continue;
            }
            //如果大于等于2代表十位数则拼接十个
            if (length >= 2) {
                if (i == 0) {
                    String shi;
                    if ("0".equals(String.valueOf(tChars[1]))) {
                        shi = aChar + ",十";
                    } else {
                        shi = aChar + ",十,";
                    }
                    sb.append(shi);
                }
                if (i == 1) {
                    if (!"0".equals(aChar)) {
                        sb.append(aChar);
                    }
                }
                continue;
            }
            //如果大于等于1代表个位数则拼直接拼接当前字符
            if (length >= 1) {
                if (i == 0) {
                    sb.append(aChar);
                }
            }
        }
        return sb.toString();
    }
}
